<?php
namespace app\index\controller;

use app\index\model\Archives;
use app\index\model\ArchivesCategory;
use think\Request;

class Count extends Common
{
    protected $request;
    protected $ac_model;
    protected $a_model;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->request = $request;
        $this->ac_model = new ArchivesCategory();
        $this->a_model = new Archives();
    }

    // 统计报表
    public function index()
    {
        $this->getAuth();
        
        $set_cate_id = session('set_cate_id') ? session('set_cate_id') : 0;
        if(empty($set_cate_id)){
            $show_path = '请选择目录';
            $cate_type = 0;
        }else{
            $c_data = $this->ac_model->where(array('cate_id'=>$set_cate_id))->find();
            if(empty($c_data)){
                $show_path = '请选择目录';
            }else{
                $path = $this->getFilePathUrl1($set_cate_id,$c_data['cate_type']);
                $show_path = $path['path'];
            }
            $cate_type = $c_data['cate_type'];
        }

        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time = date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];

        if ($prevdata !== '统计报表') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Count/index',   
                'log_desc' => '统计报表',   //默认空
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $res = db('_user_log')
                ->insert($data);
        }

        return view('index@Count/index',['show_path'=>$show_path,'cate_type'=>$cate_type,'cate_id'=>$set_cate_id]);
    }

    //统计容量
    public function rongl(){
        $set_cate_id = session('set_cate_id') ? session('set_cate_id') : 0;
        if(empty($set_cate_id)){
            $show_path = '请选择目录';
            $cate_type = 0;
        }else{
            $c_data = $this->ac_model->where(array('cate_id'=>$set_cate_id))->find();
            if(empty($c_data)){
                $show_path = '请选择目录';
            }else{
                $path = $this->getFilePathUrl1($set_cate_id,$c_data['cate_type']);
                $show_path = $path['path'];
            }
            $cate_type = $c_data['cate_type'];
        }


        return view('index@Count/rongl',['show_path'=>$show_path,'cate_type'=>$cate_type,'cate_id'=>$set_cate_id]);

    }
    //选择目录
    public function addFile(){

        $ac_data = $this->ac_model->where(array('cate_type'=>1))->select();
        if($ac_data) {
            $cate_tree = recursionCate($ac_data, 'cate_id', 0, 'cate_parent_id');
            $tree = createCateTree($cate_tree, 'cate_id', 'cate_parent_id', 'cate_name');
        }else{
            $tree = '';
        }
        return view('index@Count/addFile',['tree'=>$tree,'date'=>date('Y-m-d')]);
    }
    //设置目录
    public function setFile(){
        if($this->request->instance()->isPost()) {
            $cate_id = $this->request->post('cate_id');
            if($cate_id) {
                session('set_cate_id', $cate_id);
                jsonOut(1,'目录选择成功');
            }else{
                jsonOut(0,'请选择目录');
            }
        }
    }
    //获取文档
    public function getFileNum(){
        $cate_id = session('set_cate_id') ? session('set_cate_id') : 0;
        if(!empty($cate_id)){
            $ac_data = $this->ac_model->getDataByCondition(array('cate_id'=>$cate_id));
            //查询下面目录对应的数据
            $acs_data = $this->ac_model->getListByCondition(array('cate_parent_id'=>$cate_id));

            $ac_num = $this->a_model->getCountByCondition(array('category_id'=>$cate_id));
            $cdata[0] = $ac_data['cate_name'];
            $cnum[0] = $ac_num;
            if(!empty($acs_data)){
                for($i=0;$i<count($acs_data);$i++){
                    $ac_nums = $this->a_model->getCountByCondition(array('category_id'=>$acs_data[$i]['cate_id']));
                        $cdata[$i+1] = $acs_data[$i]['cate_name'];
                        $cnum[$i+1] = $ac_nums;
                }
            }
            jsonOut(1,'查询成功',array('cdata'=>$cdata,'cnum'=>$cnum));
        }
    }
    //获取文件下下面文件的总容量
    public function getFileRongl(){
        $cate_id = session('set_cate_id') ? session('set_cate_id') : 0;
        if(!empty($cate_id)){
            $ac_data = $this->ac_model->getDataByCondition(array('cate_id'=>$cate_id));
            //查询下面目录对应的数据
            $acs_data = $this->ac_model->getListByCondition(array('cate_parent_id'=>$cate_id));

            $ac_num = $this->a_model->field('SUM(arc_file_size) as size')->where(array('category_id'=>$cate_id))->find();
            $cdata[0] = $ac_data['cate_name'].'('.count_size($ac_num['size']).')';
            $cnum[0] = $ac_num['size'] ? $ac_num['size'] : 0;
            if(!empty($acs_data)){
                for($i=0;$i<count($acs_data);$i++){
                    $ac_nums = $this->a_model->field('SUM(arc_file_size) as size')->where(array('category_id'=>$acs_data[$i]['cate_id']))->find();
                    $cdata[$i+1] = $acs_data[$i]['cate_name'].'('.count_size($ac_nums['size']).')';
                    $cnum[$i+1] = $ac_nums['size'] ? $ac_nums['size'] : 0;
                }
            }
            jsonOut(1,'查询成功',array('cdata'=>$cdata,'cnum'=>$cnum));
        }
    }

}
